package com.yijiaganxi.services.web.banner;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.yijiaganxi.common.entity.banner.YjgxBanner;
import org.yijiaganxi.common.res.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.services.service.banner.YjgxBannerService;
import java.util.Date;
import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 轮播管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Validated
@RestController
@RequestMapping("/services/yjgxBanner/"+GlobalConstants.WEB_FLAG)
public class YjgxBannerController {

	@Autowired
	public YjgxBannerService  bannerService;
	
	/**
	 * 获取所有轮播图
	 * @return
	 */
	@SentinelResource(value = "testSentinel",blockHandler = "testSentinelBlockHandler1",fallback = "testFallback1")
	@GetMapping("getAllBannerList")
	public R getAllBannerList() {
		return R.opertionListResponse( bannerService.selectList(new EntityWrapper<YjgxBanner>()));
	}
	public R testSentinelBlockHandler1(BlockException ex) {
		return R.error("  你被 blockHandler 了 ");
	}
	public R testFallback1() {
		return R.error("  你被 fallback 了 ");
	}
	/**
	 * 根据条件查询轮播
	 * @return
	 */
	@GetMapping("getBannerListByCondition")
	public Object getBannerListByCondition(String name,Integer status) {
		EntityWrapper<YjgxBanner> entityWrapper = new EntityWrapper<YjgxBanner>();
		if(StringUtils.isNotBlank(name))
			entityWrapper.eq("name", name);
		if(status !=null)
			entityWrapper.eq("status", status);
		
		return R.opertionListResponse( bannerService.selectList(entityWrapper));
	}
	
	/**
	 * 添加轮播
	 * @param banner
	 * @return
	 */

	@PostMapping("addBanner")
	public R addBanner(@RequestBody YjgxBanner banner) {
		return R.ok(bannerService.test(banner).toString());
	}
	
	/**
	 * 更新轮播
	 * @param banner
	 * @return
	 */
	@PostMapping("updateBanner")
	public R updateBanner(YjgxBanner banner) {
		if(banner.getId()==null)
			return R.error("Id 更新操作必填");
		banner.setModifyDate(new Date());
		return R.insertOrUpdateResponse(bannerService.update(banner, new EntityWrapper<YjgxBanner>()
				.eq("id", banner.getId())));
	}
	
	/**
	 * 删除轮播
	 * @param id
	 * @return
	 */
	@PostMapping("deleteBanner")
	public R deleteBanner(@NotBlank(message = "id不能为空") String id) {
		return R.insertOrUpdateResponse(bannerService.deleteById(id));
	}
}

